#include "m_phpredis.h"
#include "segment.h"
#include "span.h"
#include "helper.h"
#include "agent.h"
#include "common.h"
// #include "skywalking_client.h"

#include <string>

void monitorpro_phpredis_handler(zend_execute_data *execute_data, zval *return_value, long start_micro_time, long end_micro_time, std::string class_name, std::string function_name)
{
    if (function_name == "__construct" || function_name == "__destruct")
    {
        return;
    }

    zval obj = execute_data->This;
    redis_object *redis = PHPREDIS_ZVAL_GET_OBJECT(redis_object, &obj);
    int port = redis->sock->port;
    std::string ip = ZSTR_VAL(redis->sock->host);
    std::string addr = ip + ":" + std::to_string(port);
    std::string query = class_name + "->" + function_name;
    std::string err = "";

    // SkywalkingClient *skywalking_client = SkywalkingClient::newInstance();
    // Segment *segment = skywalking_client->getSegment();
    // auto *span = segment->createSpan(SkySpanType::Exit, SkySpanLayer::Database, 8003);
    // span->setOperationName(class_name + "->" + function_name);
    // span->addTag("db.type", "redis");
    // span->addTag("db.driver", "phpredis");
    // span->addTag("redis.command", function_name);
    // span->setPeer(addr);
    // span->setStartTime(start_micro_time / 1000);
    // span->setEndTime(end_micro_time / 1000);

    push_redis_data(addr, query, err, end_micro_time - start_micro_time);
}